public class DijkstraRouter {
    static final int MAX = 10000;

    public static int[] Dijkstra(int[][] weight, int start) {
        // 图拓扑中顶点个数
        int length = weight.length;
        // 存放从start点开始到各顶点的距离
        int[] shortPath = new int[length];
        shortPath[0] = 0;
        // 存放各个顶点的访问情况（1表示已经访问过）
        int[] visited = new int[length];
        visited[0] = 1;
        // 存放从start点到各点的最短路径的字符串表示
        String[] path = new String[length];
        for (int i = 0; i < length; i++) {
            path[i] = start + "->" + i;
        }
        for (int count = 1; count < length; count++) {
            int k = -1;
            int dMin = Integer.MAX_VALUE;
            for (int i = 0; i < length; i++) {
                if (visited[i] == 0 && weight[start][i] < dMin) {
                    dMin = weight[start][i];
                    k = i;
                }
            }
            shortPath[k] = dMin;
            visited[k] = 1;
            for (int i = 0; i < length; i++) {
                if (visited[i] == 0 && weight[start][k] + weight[k][i] < weight[start][i]) {
                    weight[start][i] = weight[start][k] + weight[k][i];
                    path[i] = path[k] + "->" + i;
                }
            }
        }
        for (int i = 0; i < length; i++) {
            System.out.println("从" + start + "出发到" + i + "的最短路径为：" + path[i] + " = " + shortPath[i]);
        }
        return shortPath;
    }

    public static void main(String[] args) {
        int[][] weight = {
                {0, 3, MAX, 7, MAX},
                {3, 0, 4, 2, MAX},
                {MAX, 4, 0, 5, 4},
                {7, 2, 5, 0, 6},
                {MAX, MAX, 4, 6, 0}
        };
        int start = 0;
        int[] dijkstra = Dijkstra(weight, start);
    }
}
